package com.yazi.mxz.behavior.controller;


import javax.annotation.Resource;

import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.yazi.mxz.behavior.common.BaseController;
import com.yazi.mxz.behavior.entity.repository.GoodsClassify;
import com.yazi.mxz.behavior.jackson.filter.GoodsClassifyFilter;
import com.yazi.mxz.behavior.repository.GoodsClassifyRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("goodsClassifyController")
@RequestMapping("/goodsClassify")
public class GoodsClassifyController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(GoodsClassifyController.class);

	@Resource
	GoodsClassifyRepository goodsClassifyRepository;

	@RequestMapping("/listByCompanyId/json")
	@ResponseBody
	public ListResult<?> listByCompanyIdJson(long companyId) {
		log.debug("获取企业商品类目: companyId={}", companyId);
		ListResult<GoodsClassify> jsonResult = new ListResult<GoodsClassify>();
		jsonResult.setFilter(GoodsClassifyFilter.get());
		try{
			jsonResult.setResult(goodsClassifyRepository.findByCompanyIdAndDeepOrderBySort(companyId, 0));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取企业商品类目列表异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/listByParentId/json")
	@ResponseBody
	public ListResult<?> listByParentIdJson(long parentId) {
		log.debug("获取子级商品类目: parentId={}", parentId);
		ListResult<GoodsClassify> jsonResult = new ListResult<GoodsClassify>();
		jsonResult.setFilter(GoodsClassifyFilter.get());
		try{
			jsonResult.setResult(goodsClassifyRepository.findByParentIdOrderBySort(parentId));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取子级商品类目列表异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	
	@RequestMapping("/one/json")
	@ResponseBody
	public ObjectResult<?> ontJson(long regionId) {
		log.debug("获取商品类目: regionId={}", regionId);
		ObjectResult<GoodsClassify> jsonResult = new ObjectResult<GoodsClassify>();
		jsonResult.setFilter(GoodsClassifyFilter.get());
		try{
			
			jsonResult.setResult(goodsClassifyRepository.findOne(regionId));
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取商品类目异常", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

}
